package com.shilei.c05_1_ThreadLocalAndRefType;

import java.util.concurrent.TimeUnit;

/**
 * @description:  每个线程独有一份数据
 *                  tl.set()
 *                 原理 是往调用者的线程的ThreadLocalMap里  put
 *
 * @author: 81271
 * @date: 2020-06-20 15:18
 */
public class TestThreadLocal2 {
    static ThreadLocal<Person2> tl = new ThreadLocal<>();


    public static void main(String[] args) {
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(tl.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                Person2 person2 = new Person2();
                person2.name = "ooxx";
                tl.set(person2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


    }

}

class Person2{
    String name = "xxoo";
}